package com.panda.command;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Console {


    public static Charset charset = Charset.forName("UTF-8");

    public static void log(String msg) {
        System.out.println(msg);
        System.console().printf("%s\nPANDA:", msg);
    }

    public static void warning(String msg) {
        System.out.println(msg);
        System.console().printf("WARNING%s\nPANDA:", msg);
    }


    public static void err(String msg) {
        System.console().printf("ERROR:%s\nPANDA:", msg);
    }


    //空的占位
    private static Operation rootOperation = new Operation() {

        @Override
        boolean canHandle(String ope) {
            return false;
        }

        @Override
        void doHandle(String ope, List<String> args) {

        }

    };

    static {

        rootOperation


                //主要解析链
                .next(new CompileOperation()).next(new PushOperation())


                .next(new Operation() {
                    @Override
                    boolean canHandle(String ope) {
                        return true;
                    }

                    @Override
                    void doHandle(String ope, List<String> args) {

                        throw new RuntimeException("不支持的操作:" + ope);

                    }
                });

    }

    private static Pattern cp = Pattern.compile(" *([a-z]+)( |$)");
    private static Pattern ap = Pattern.compile(" +-[a-z]+( *[^ ]*)?");

    public static void start(String[] args) {

        for (String s : args) {

            if (s.startsWith("charset:")) {

                String cs = s.split("charset:")[1];

                charset = Charset.forName(cs);

            }

        }


        String line;

        String last = null;

        System.console().printf("PANDA:");

        while (true) {

            line = System.console().readLine();

            if (line.equals("EOF")) {
                break;
            }
            System.console().printf("PANDA:");

            if (line.equals("")) {
                if (last != null) {
                    command(last);
                }
            } else {
                command(line);
                last = line;
            }

        }

    }


    public static void command(String command) {

        Matcher matcher = cp.matcher(command);

        if (matcher.find()) {

            String ope = matcher.group(1);

            if (!"".equals(ope.trim())) {

                matcher = ap.matcher(command);

                List<String> as = new ArrayList<>();

                while (matcher.find()) {
                    as.add(matcher.group());
                }

                try {
                    rootOperation.handle(ope, as);
                } catch (Exception e) {

                    e.printStackTrace();
                    err(e.getMessage());

                }


            }


        } else {
            err("格式错误:" + command);
        }

    }

}

